package me.ling;

import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.BinaryOperator;


/**
 * JDK7 匿名内部类写法
 */
public class HelloLambda {
    public static void main(String[] args) {
        //no arg
        /**
         * JDK7 匿名内部类写法
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("JDK7");

            }
        }).start();
        /**
         * JDK8 lambda
         */
        new Thread(() -> {
            System.out.println("JDK8");
        }).start();
        // have arg JDK7
        List<String> list = Arrays.asList("leaker","is","champion");
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                if (s1 == null)
                    return -1;
                if (s2 == null)
                    return 1;
                return s1.length() - s2.length();
            }
        });
        // have arg JDK8
        Collections.sort(list, (s1,s2)->{
            if(s1==null)
                return -1;
            if(s2==null)
                return 1;
            return s1.length()-s2.length();
        });

        // Lambda表达式的书写形式
        // 1展示了无参函数的简写；
        Runnable run = () -> System.out.println("Hello World");// 1
        // 2处展示了有参函数的简写，以及类型推断机制；
        ActionListener listener = event -> System.out.println("button clicked");// 2
        //3是代码块的写法；
        Runnable multiLine = () -> {// 3 代码块
            System.out.print("Hello");
            System.out.println(" Hoolee");
        };
        //4和5再次展示了类型推断机制。
        BinaryOperator<Long> add = (Long x, Long y) -> x + y;// 4
        BinaryOperator<Long> addImplicit = (x, y) -> x + y;// 5 类型推断
    }
}
